/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.gui.security;

import it.polimi.registration.business.security.boundary.CalendarManager;
import it.polimi.registration.business.security.boundary.NotificationManager;
import it.polimi.registration.business.security.boundary.UserManager;
import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.Invitation;
import it.polimi.registration.business.security.entity.Notification;
import it.polimi.registration.business.security.entity.User;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author miglie
 */
@Named
@RequestScoped
public class UserBean {

    @EJB
    private UserManager um;

    @EJB
    private CalendarManager cm;
    
    @EJB
    private NotificationManager nm;
    
    @PersistenceContext
    EntityManager em;
    
    public UserBean() {
    }
    
    public String getName() {
    	User u = um.getLoggedUser();
        return u.getFirstName() + " " + u.getLastName();
    }
    
    public String getEmail() {
    	User u = um.getLoggedUser();
    	return u.getEmailAddress();
    }
    
    public String getAvatar() {
    	User u = um.getLoggedUser();
    	if(u.getAvatar() == null || u.getAvatar().toString().isEmpty())
    		return "/imgs/profile.png";
    	else
    		return u.getAvatar().toString();
    }
    
    public String getVisibilityCalendar() {
    	User u = um.getLoggedUser();
    	return u.getCalendar().isPublic() ? "Public" : "Private";
    }
    
    public String getInverseVisibilityCalendar() {
    	User u = um.getLoggedUser();
    	return !u.getCalendar().isPublic() ? "public" : "private";
    }
    
    public List<Event> getNextEvents() {
    	User u = um.getLoggedUser();
    	return getNextEvents(u);
    }
    	
    public List<Event> getNextEvents(User u) {
    	List<Event> le = u.getEvents();
    	List<Event> res = new ArrayList<Event>();
    	Calendar now = new GregorianCalendar();
    	
    	res.addAll(this.getMyEvents());
    	res.addAll(this.getAcceptedEvents());
    	
    	sortList(res);
    	
    	return res;
    }

	public List<Event> getMyEvents() {
    	User u = um.getLoggedUser();
    	//List<Event> le = getNextEvents(u);
    	List<Event> mine = new ArrayList<>();
    	Calendar now = new GregorianCalendar();
    	
    	/*for(Event e : le) {
    		if(e.getOwner().equals(u)) {
    			mine.add(e);
    		}
    	}*/
    	
    	List<Event> le = (List<Event>) em.createQuery("SELECT t FROM event t where t.owner = :value1")
                .setParameter("value1", u).getResultList();
    	
    	for(Event e : le) {
    		if(now.before(e.getDate())) {
    			mine.add(e);
    		}
    	}
    	
    	sortList(mine);
    	
    	return mine;
    }
    
    public List<Event> getAcceptedEvents() {
    	User u = um.getLoggedUser();
    	List<Event> res = new ArrayList<Event>();
    	List<Invitation> ln;
    	Calendar now = new GregorianCalendar();
    	
    	/*for(Notification n : ln) {
    		if(ln instanceof Invitation) {
    			if(((Invitation)n).getAnswer()) {
    				if(now.before(n.getEvent().getDate()))
    					res.add(n.getEvent());
    			}
    		}
    	}*/
    	
    	ln = em.createNativeQuery("SELECT * FROM notification where OWNER_ID = " + u.getId() + " and TYPE_ENTITE = 'Invitation' and answer = 1", Invitation.class).getResultList();
    	
    	for(Invitation n : ln) {
			if(now.before(n.getEvent().getDate()))
				res.add(n.getEvent());
    	}
    	
    	sortList(res);
    	
    	return res;
    }
    
    private void sortList(List<Event> l) {
		l.sort(new Comparator<Event>() {

			@Override
			public int compare(Event arg0, Event arg1) {
				return arg0.compareTo(arg1);
			}});
	}
    
    public int getNotifsUnreadNumber() {
    	User u = um.getLoggedUser();
    	List<Notification> ln = u.getNotifications();
    	int i = 0;
    	
    	for(Notification n : ln)
    		if(!n.isRead())
    			i++;
    	
    	return i;
    }
    
    public String changeVisibility() {
    	User u = um.getLoggedUser();
    	u.getCalendar().setPublic(!u.getCalendar().isPublic());
    	cm.merge(u.getCalendar());
		return "/user/index.xhtml?faces-redirect=true";
    }
    
    public String markAsRead() {
    	User u = um.getLoggedUser();
    	
    	for(Notification n : u.getNotifications()) {
    		n.setRead(true);
    		nm.merge(n);
    	}
    	
    	return "/user/index.xhtml?faces-redirect=true";
    }
    
    public List<Notification> getNotifications() {
    	return um.getLoggedUser().getNotifications();
    }
}
